package app.blog.entity;

import slap.berkeleydb.BDB;
import app.blog.Blog;

import com.sleepycat.persist.PrimaryIndex;
import com.sleepycat.persist.SecondaryIndex;
import com.sleepycat.persist.model.Entity;
import com.sleepycat.persist.model.PrimaryKey;
import com.sleepycat.persist.model.Relationship;
import com.sleepycat.persist.model.SecondaryKey;


@Entity
public class PostJoinTag {
	
	protected static PrimaryIndex<Long, PostJoinTag> primaryIndex = 
		BDB.instanse(Blog.bdbName).getEntityStore().getPrimaryIndex(Long.class, PostJoinTag.class);
	
	protected static SecondaryIndex<Integer, Long, PostJoinTag> indexOnPostId = 
		BDB.instanse(Blog.bdbName).getEntityStore().getSecondaryIndex(primaryIndex, Integer.class, "post_id");
	
	protected static SecondaryIndex<Integer, Long, PostJoinTag> indexOnTagID = 
		BDB.instanse(Blog.bdbName).getEntityStore().getSecondaryIndex(primaryIndex, Integer.class, "tag_id");
	
	public static void persist(PostJoinTag join) {
		primaryIndex.put(join);
	}
	
	private PostJoinTag(){
	}
	
	public PostJoinTag(Integer post_id, Integer tag_id) {
		this.post_id = post_id;
		this.tag_id = tag_id;
	}

	@PrimaryKey(sequence = "pjt-id")
    Long id;
	
	@SecondaryKey(relate = Relationship.MANY_TO_ONE, relatedEntity = Article.class)
    public Integer post_id;
	
	@SecondaryKey(relate = Relationship.MANY_TO_ONE, relatedEntity = Tag.class)
    public Integer tag_id;

}
